﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using jaysJourney_v01.Common;
using jaysJourney_v01.Utils.Blocks.Interactive.Types;
using jaysJourney_v01.States;

namespace jaysJourney_v01.Utils.Blocks.Interactive
{
    class InteractiveBlockManager
    {
        /**
         * MEMBER VARIABLES
         * */

        // From Parent

        private SpriteBatch parentSpriteBatch;
        private PlayState lvl;

        // Common

        public List<InteractiveBlock> blockList;

        /**
         * METHODS
         * */

        // Constructor

        public InteractiveBlockManager(SpriteBatch parentSpriteBatch, PlayState level)
        {
            this.parentSpriteBatch = parentSpriteBatch;
            this.lvl = level;

            this.blockList = new List<InteractiveBlock>();
        }

        // MAIN UPDATE

        public void update(GameTime gameTime)
        {
            for (int i = 0; i < blockList.Count; i++)
            {
                blockList[i].updateBase(gameTime); // update

                if (blockList[i] is SpikeBlock && ((SpikeBlock)blockList[i]).gotTouched) // kill jay
                {
                    lvl.jay.killJay();
                }

                if (blockList[i].isBlockToRemove()) // remove
                {
                    lvl.getLevel().curLevel[(int)(blockList[i].getPosition().X / CONST.BLOCKSIZE), (int)(blockList[i].getPosition().Y / CONST.BLOCKSIZE)] = FieldType.Nothing;
                    blockList.Remove(blockList[i]);
                }
            }
        }

        // MAIN DRAW

        public void draw(GameTime gameTime, Vector2 curZero)
        {
            for (int i = 0; i < blockList.Count; i++)
            {
                blockList[i].drawBase(gameTime, curZero);
            }
        }

        // add InteractiveBlock to list

        public void addBlock(InteractiveBlock block)
        {
            this.blockList.Add(block);
        }

        // trigger block on given position

        public void triggerBlock(Point position)
        {
            Vector2 pos = new Vector2(position.X, position.Y);

            for (int i = 0; i < blockList.Count; i++)
            {
                if (blockList[i].getPosition() == pos * CONST.BLOCKSIZE)
                {
                    blockList[i].triggerBase();
                    if (blockList[i] is InvisBlock)
                    {
                        //blockList[i].resetTriggerAnimation();
                    }
                    //Debug.WriteLine("regular");
                    break;
                }
                else if (blockList[i] is GoalBlock)
                {
                    //Debug.WriteLine(blockList[i].getPosition().Y + " " + pos.Y * CONST.BLOCKSIZE);
                    if (blockList[i].getPosition().X == pos.X * CONST.BLOCKSIZE && blockList[i].getPosition().Y + 2 * CONST.BLOCKSIZE == pos.Y * CONST.BLOCKSIZE)
                    {
                       // Debug.WriteLine("goal");
                        blockList[i].triggerBase();
                        break;
                    }
                }
            }
        }

        // empty list

        public void emptyBlockList()
        {
            this.blockList = new List<InteractiveBlock>();
        }
    }
}
